package bairui.international.weixin.portal.miniapp.controller.user;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.domain.jpa.TB_UserGradeRecord;
import bairui.international.model.type.Type_UserGrade;
import bairui.international.service.Service_User;
import bairui.international.service.Service_UserGradeRecord;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 积分列表
 *
 * @generated
 * 
 * @see STest_C_User_ScorePage_Get 测试
 *
 */
@Controller
public class C_User_ScorePage_Get
 extends SC_User_ScorePage_Get 
{
	
	@Autowired private Service_UserGradeRecord service_UserGradeRecord;
	@Autowired private Service_User service_User;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam Integer startPage,
		@RequestParam String skey
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		//
		Page<TB_UserGradeRecord> page = service_UserGradeRecord.page(
				it->it.user.eq(user), 
				PageRequest.of(startPage, 5),
				it->new QSort(it.id.desc())
			);
		return page
				.map(it->new Output(
						it.getScore(),
						null==it.getCreateTime() ? "" : new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(it.getCreateTime()),
						it.getCurrentScore(),
						it.getType().getComment(),
						it.getType()
					));
	}
	
	@AllArgsConstructor
	public static class Output {
		private @Getter Double score;
		private @Getter String submitDate;
		private @Getter Double currentScore;
		private @Getter String typeComment;
		private @Getter Type_UserGrade type;
	}
	
}
